{"componentChunkName":"component---src-templates-post-jsx","path":"/IoC와 DI/","result":{"data":{"site":{"siteMetadata":{"title":"SUbbb"}},"markdownRemark":{"id":"4101ebe6-43c8-571b-b984-a63d64e55d2b","excerpt":"🧷 IoC Inversion of Controller의 약자로, 제어의 역전이라는 의미이다. 일반적인 제어는 객체의 생명주기(생성 - 설정 - 초기화 - 소멸)를 개발자가 주체가 되어 담당하는 것이다. 그렇다면 제어의 역전은 그 반대로 스프링 프레임워크가 주체가 되어 객체의 생명주기를 담당하는 것이다. (정확하게는 스프링 내부에 존재하는 IoC 컨테이너에…","html":"<h2>🧷 IoC</h2>\n<p>Inversion of Controller의 약자로, <strong>제어의 역전</strong>이라는 의미이다.</p>\n<p>일반적인 제어는 객체의 생명주기(생성 - 설정 - 초기화 - 소멸)를 개발자가 주체가 되어 담당하는 것이다. 그렇다면 <strong>제어의 역전</strong>은 그 반대로 <strong>스프링 프레임워크가 주체가 되어 객체의 생명주기를 담당</strong>하는 것이다. (정확하게는 스프링 내부에 존재하는 IoC 컨테이너에서 담당한다.)</p>\n<p>제어가 역전되었을 때, 나오는 상황들은 다음과 같다.</p>\n<ul>\n<li>\n<p>단순 자바를 사용하다가 스프링을 사용하게 될 경우, 객체 생성 시 <code class=\"language-text\">new</code> 라느느 키워드를 더 이상 사용할 필요가 없다.</p>\n<ul>\n<li>해당 키워드가 없어도 스프링에서 객체를 생성할 수 있도록 도와주기 때문이다.</li>\n</ul>\n</li>\n</ul>\n<blockquote>\n<p>즉, 개발자들이 해야 할 잡일들을 스프링이 대신하여 개발자가 개발에 더 집중할 수 있는 환경을 조성해준다.</p>\n</blockquote>\n<hr>\n<h2>🧷 DI</h2>\n<p>Dependency Injection의 약자로, <strong>의존성 주입</strong>이라는 의미이다.</p>\n<p>보통 객체를 생성할 때는 클래스 내부에서 값을 넣어주는데, 내부에서 값을 넣어주는 것이 아닌 <strong>외부에서 넣어주는 것</strong>이다.</p>\n<blockquote>\n<p>이해를 돕기 위한 예제로, 케이크 전문점에서 케잌 맞춤 제작을 한다고 가정한다..</p>\n<ul>\n<li>\n<p>여기서 케잌 전문점은 IoC 컨테이너라고 비유할 수 있다.</p>\n<ul>\n<li>기본적인 구조를 기반으로 케이크 주문자의 명세서에 맞게 맞춤 제작을 해주고 보관까지 하고 있다가 주문자가 찾으러 오면 완성품을 제공하기 때문이다.</li>\n</ul>\n</li>\n<li>그렇다면 DI는 케잌 주문자가 아닌 외부(케잌 전문점)에서 주문 명세서에 맞게 맞춤 제작하는 것을 비유할 수 있다. </li>\n</ul>\n</blockquote>\n<p>의존성 주입을 하는 이유는 \"<strong>객체들간의 의존성을 낮추며 유연성을 높이기 위함</strong>\"이다. 시스템의 규모가 커지더라도 객체들간의 의존성을 낮춰 코드를 수정하는데 들이는 시간을 줄일 수 있다.</p>\n<blockquote>\n<p>의존성 주입은 어느 프레임워크에서든 가능하다. 하지만 의존성이 많아지면 그만큼 관리 코드도 많아지게 된다. 그래서 이를 편리하게 해주는 DI 프레임워크 중 Spring, Dagger 등을 사용한다.</p>\n</blockquote>\n<h3>🗝 의존성 주입 방법</h3>\n<p>총 4가지 방법이 있다.</p>\n<h4>🔧 생성자 주입 (constructor injection)</h4>\n<h4>🔧 세터 주입 (setter injection)</h4>\n<h4>🔧 메서드 주입 (method call injection)</h4>\n<h4>🔧 인터페이스 주입 (interface injection)</h4>\n<hr>\n<h2>📕 참고</h2>\n<ul>\n<li><a href=\"https://jforj.tistory.com/50\">[Spring] 제어의 역전(IoC), 의존성 주입(DI)</a></li>\n<li><a href=\"https://umbum.dev/873\">의존성 주입(DI, Dependency Injection)이란?</a></li>\n<li><a href=\"https://hue-dev.site/springframework/2021/05/03/Dependency-Injection-%EC%9D%B4-%EB%AD%90%EC%97%90%EC%9A%94.html\">의존성 주입, Dependency Injection에 관한 고찰🔎 With OOP</a></li>\n</ul>","frontmatter":{"title":"💬 IoC와 DI","date":"April 27, 2022","update":"April 27, 2022","tags":["개발상식","Java","IoC","DI"],"series":"💻 BackEnd"},"fields":{"slug":"/IoC와 DI/","readingTime":{"minutes":3.76}}},"seriesList":{"edges":[{"node":{"id":"08d09624-7fa6-5d93-9623-abb70d863931","fields":{"slug":"/[BackEnd]JWT/"},"frontmatter":{"title":"💻 JWT"}}},{"node":{"id":"fa6720c2-ab20-5a55-b914-bbf10ec8fda0","fields":{"slug":"/[BackEnd]Sync&aSync/"},"frontmatter":{"title":"💻 동기 및 비동기 처리"}}},{"node":{"id":"72a19297-193d-5bb7-895a-44f328517242","fields":{"slug":"/[BackEnd]REST/"},"frontmatter":{"title":"💻 REST"}}},{"node":{"id":"61c43f1b-d1df-5602-9b51-8fe6fa9d3b01","fields":{"slug":"/[BackEnd]객체지향생활체조/"},"frontmatter":{"title":"💻 객체지향 생활체조"}}},{"node":{"id":"871dad9f-422b-50ed-8c98-d441dc97e4d0","fields":{"slug":"/[BackEnd]Interceptor/"},"frontmatter":{"title":"💻 Interceptor"}}},{"node":{"id":"b945159a-d6ce-5269-a463-898869856e12","fields":{"slug":"/[BackEnd]Docker배포/"},"frontmatter":{"title":"💻 Docker를 이용한 Springboot 배포"}}},{"node":{"id":"4101ebe6-43c8-571b-b984-a63d64e55d2b","fields":{"slug":"/IoC와 DI/"},"frontmatter":{"title":"💬 IoC와 DI"}}},{"node":{"id":"80b1cfb1-9f95-5921-bac9-4c540ad89c1e","fields":{"slug":"/[Backend]WebServer&WAS/"},"frontmatter":{"title":"💻 Web Server와 WAS"}}}]},"previous":{"fields":{"slug":"/[면접]Garbage Collection/"},"frontmatter":{"title":"💬 Garbage Collection"}},"next":{"fields":{"slug":"/MSA/"},"frontmatter":{"title":"💬 Micro Service Architecture"}}},"pageContext":{"id":"4101ebe6-43c8-571b-b984-a63d64e55d2b","series":"💻 BackEnd","previousPostId":"8dd43555-9ec5-5e07-8f64-f679a91ecf76","nextPostId":"2f5814e4-8326-50ea-b7e9-9abe28cf8b76"}},"staticQueryHashes":[]}